<template>
  <div class="login_type">
    <img src="../../assets/background/1.jpg" alt="">
    <div class="login_box">
      <div class="avatar_box">
        <img
          src="../../assets/logo/1.jpg"
          alt=""
        >
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="loginForm_ref"
        :model="loginForm"
        :rules="loginForm_rules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          />
        </el-form-item>
        <!-- button -->
        <el-form-item class="btn_ty">
          <el-button
            type="primary"
            @click="login"
          >登录</el-button>
          <el-button
            type="info"
            @click="resetloginForm"
          >重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
    /* 登录表单的绑定对象 */
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      /* 表单信息验证 */
      loginForm_rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /* 重置表单 */
    resetloginForm() {
      /* console.log(this) */
      this.$refs.loginForm_ref.resetFields()
    },
    login() {
      this.$refs.loginForm_ref.validate(async valid => {
        if (!valid) return
        /* 用到了接口login */
        await this.$store.dispatch('user/login', this.loginForm)
        /* console.log(this.$store.getters.token) */
        /* 用token记录登录令牌，方便后续有权限访问数据库 */
        if (!window.sessionStorage.getItem('TokenKey')) { return this.$message.error('用户名或密码错误!') }
        this.$message.success('登录成功，欢迎你！')
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang='less' scoped>
.login_type {
  background-color: rgb(91, 94, 97);
  height: 100%;
  img {
    height: 100%;
    width: 100%;
  }
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 150px;
    width: 150px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 1px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btn_ty {
  display: flex;
  justify-content: center;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}
</style>
